*For Margaret dropbox folders 
global NCREIF "/Users/BeckaBrolinson/Dropbox/NCREIF/data" 
global build 	"$NCREIF/build" 
global analysis "$NCREIF/analysis"
global results 	"$analysis/results"
global figures 	"$analysis/figures" 

*------------------------------------------------------------------------------*
*	Step 13- Make Pre match and post-match balance tables 	   *
*------------------------------------------------------------------------------*	


	use "$build/01_annualizeddatawcontrol.dta", replace

	gen ttest_var = . 
	replace ttest_var = 1 if treat==1 & post==0 
	replace ttest_var = 0 if treat==0 & year<=2009 

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	gen nra1000s = nra / 1000
	

* Step 1: Label variables with the text you want to appear in the table *

	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	label var nooffloors "Number of Floors"
	label var noofunits "Number of Units"
	label var noofbuildings "Number of Buildings"
	label var nra1000s "Net Rentable Area (1000s)"
	label var lastrenovatedyear "Last Renovated Year"
	label var latitude  "Latitude"
	label var longitude "Longitude"

*Step 2 Make a local with the variables I am doing the ttest for 
	loc covars "rl_yr_utilpersf rl_yr_rentpersf real_elecprice real_gasprice Unemployment HDD1000s CDD1000s used_space1000s percentleased real_capex_ti_sqft real_capex_bldimp_sqft sqft1000s yrbuilt Covered_E nooffloors noofunits noofbuildings nra1000s lastrenovatedyear latitude longitude"
	

	   
*Step 3 Estimate means to store in matrices to put into tables 

	*Estimate the means and SDs 
	*clear stored estimates 
	eststo clear
	estpost sum `covars' if ttest_var==1
	
	matrix mean_cert=e(mean)
	matrix list mean_cert
	
	estpost sum `covars' if ttest_var==0
	matrix mean_uncert = e(mean) 
	matrix list mean_uncert 
	
	stddiff `covars', by(ttest_var)
	matrix stddiff = r(stddiff)
	matrix stddifftrans = stddiff'*100
	matrix list stddifftrans
	matrix colnames stddifftrans = rl_yr_utilpersf rl_yr_rentpersf real_elecprice real_gasprice Unemployment HDD1000s CDD1000s used_space1000s percentleased real_capex_ti_sqft real_capex_bldimp_sqft sqft1000s yrbuilt Covered_E nooffloors noofunits noofbuildings nra1000s lastrenovatedyear latitude longitude
	

*Step 4, run ttest 
	*Run ttest 
	estpost ttest `covars', by(ttest_var)

*Step 5 add stored means to ttest results 

	estadd matrix mean_cert
	estadd matrix mean_uncert
	estadd matrix stddifftrans

	esttab 
*Step 6 Locals for column names 
			
	loc colnames " "\multicolumn{1}{c}{Mean Uncertified Pre-2009}" "\multicolumn{1}{c}{Mean Certified Pre-Cert}" "\multicolumn{1}{c}{Diff.}" "\multicolumn{1}{c}{Std. Error}" "\multicolumn{1}{c}{Std. Diff (\%)}" "N" "

	

*Step 6 Output into table 

    esttab using "$results/11_balancetest_unmatched.tex",  label replace booktabs ///
	nonotes nogaps /// removes notes from bottom of table 
	nonumbers /// 
	nomtitles /// 
	alignment(SSSSSS) ///
	star(* 0.05)  ///
	cells("mean_cert(fmt(%9.3f)) mean_uncert(fmt(%9.3f)) b(star fmt(%9.3f)) se(fmt(%9.3f)) stddifftrans(fmt(%9.3f))") ///
	title(Unmatched Sample: Balance Test \label{tab11unmatchedbalance}) ///
	nonum compress noobs nobaselevels  collabels(`colnames') ///
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	addnote("Each column reports the annual average for certified buildings and uncertfied buildings prior to certification. For buildings that are never certified we define before" ///
						"certification as before 2009, the average certification year in the sample. In column ``Diff.'' * p$<$0.05 indicates statistically significant differences between certified and" /// 
						" uncertified buildings. Column ``Std. Diff (\%)'' presents the standardized percent bias which is defined as the difference in the sample means between two groups" ///
						" as a percentage of the square root of the average sample variance. While there is no optimal level of standardized bias, empirical researchers often consider a" ///
						"standardized bias of 3–5\% to be sufficient \textcite{caliendo2008}." )


*******************************************************************************
	*Ttest for the two different matched samples 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'.dta", clear 

	gen ttest_var = . 
	replace ttest_var = 1 if treat==1 & post==0 
	replace ttest_var = 0 if treat==0 & year<=2009 

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

* Step 1: Label variables with the text you want to appear in the table *

	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 

*Step 2 Make a local with the variables I am doing the ttest for 
	loc covars "rl_yr_utilpersf rl_yr_rentpersf sqft1000s yrbuilt Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s used_space1000s percentleased real_capex_ti_sqft real_capex_bldimp_sqft"
	

*Step 3 Estimate means to store in matrices to put into tables 

	*Estimate the means and SDs 
	*clear stored estimates 
	eststo clear
	estpost sum `covars' if ttest_var==1
	
	matrix mean_cert=e(mean)
	matrix list mean_cert
	
	estpost sum `covars' if ttest_var==0
	matrix mean_uncert = e(mean) 
	matrix list mean_uncert 
	
*Step 4, run ttest 
	*Run ttest 
	estpost ttest `covars', by(ttest_var)

*Step 5 add stored means to ttest results 

	estadd matrix mean_cert
	estadd matrix mean_uncert
	
*Step 6 Locals for column names 
	
	   loc colnames " "\multicolumn{1}{c}{Mean Uncertified Pre-2009}" "\multicolumn{1}{c}{Mean Certified Pre-Cert}" "\multicolumn{1}{c}{Diff.}" "\multicolumn{1}{c}{Std. Error}" "N" "

	
*Step 6 Output into table 

	esttab using "$results/12_balancetest_matched`i'.tex",  label replace booktabs ///
	nonotes nogaps /// removes notes from bottom of table 
	nonumbers /// 
	nomtitles /// 
	alignment(SSSS) ///
	star(* 0.05)  ///
	cells("mean_cert(fmt(%9.3f)) mean_uncert(fmt(%9.3f)) b(star fmt(%9.3f)) se(fmt(%9.3f))") ///
	title(Matched Sample: Balance Test \label{tab12matchedbalance`i'}) ///
	nonum compress noobs nobaselevels  collabels(`colnames') ///
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	addnote("Standard deviations are reported in parenthesis. Each column reports the annual average and the standard deviation over " ///
						"two time periods--before certification and after certification. For buildings that are never certified we define " /// 
						"before certification as before or in 2009 and after certification as after 2009.")

}

		
		*Ttest for the two different matched samples 
	forvalues i=2/3{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'_wmatches.dta", clear 

	gen ttest_var = . 
	replace ttest_var = 1 if treat==1 & post==0 
	replace ttest_var = 0 if treat==0 & year<=2009 

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 

* Step 1: Label variables with the text you want to appear in the table *

	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 

*Step 2 Make a local with the variables I am doing the ttest for 
	loc covars "rl_yr_utilpersf rl_yr_rentpersf sqft1000s yrbuilt Covered_E real_elecprice real_gasprice Unemployment HDD1000s CDD1000s used_space1000s percentleased real_capex_ti_sqft real_capex_bldimp_sqft"
	

*Step 3 Estimate means to store in matrices to put into tables 

	*Estimate the means and SDs 
	*clear stored estimates 
	eststo clear
	estpost sum `covars' if ttest_var==1
	
	matrix mean_cert=e(mean)
	matrix list mean_cert
	
	estpost sum `covars' if ttest_var==0
	matrix mean_uncert = e(mean) 
	matrix list mean_uncert 
	
*Step 4, run ttest 
	*Run ttest 
	estpost ttest `covars', by(ttest_var)

*Step 5 add stored means to ttest results 

	estadd matrix mean_cert
	estadd matrix mean_uncert
	
*Step 6 Locals for column names 
	
	   loc colnames " "\multicolumn{1}{c}{Mean Uncertified Pre-2009}" "\multicolumn{1}{c}{Mean Certified Pre-Cert}" "\multicolumn{1}{c}{Diff.}" "\multicolumn{1}{c}{Std. Error}" "N" "

	
*Step 6 Output into table 

	esttab using "$results/12_balancetest_matched`i'.tex",  label replace booktabs ///
	nonotes nogaps /// removes notes from bottom of table 
	nonumbers /// 
	nomtitles /// 
	alignment(SSSS) ///
	star(* 0.05)  ///
	cells("mean_cert(fmt(%9.3f)) mean_uncert(fmt(%9.3f)) b(star fmt(%9.3f)) se(fmt(%9.3f))") ///
	title(Matched Sample: Balance Test \label{tab12matchedbalance`i'}) ///
	nonum compress noobs nobaselevels  collabels(`colnames') ///
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	addnote("Standard deviations are reported in parenthesis. Each column reports the annual average and the standard deviation over " ///
						"two time periods--before certification and after certification. For buildings that are never certified we define " /// 
						"before certification as before or in 2009 and after certification as after 2009.")

}

*Ttest for the two different matched samples lined up with their matched building for the 
*post year 


	
	
	forvalues i=2/14{
	use "$build/FakeNCREIFAnnualizedDatawcontrol_PostMatch`i'_wmatches.dta", clear 

	*Generate the post-variable for uncertified buildings to line up with the certification year of their
	*matched building 
	*within each pair generate the treatment year 
	*check that observations are unique 
	capture drop id_unique 
	bysort  matchcalindextreated treat year: gen id_unique = _n ==1 // al unique 
	drop id_unique
	
	by matchcalindextreated: egen firstyearrated_pair = min(firstyearrated)
	
	gen post_pair = (year>=firstyearrated_pair)
	
	gen ttest_var = . 
	replace ttest_var = 1 if treat==1 & post_pair==0 
	replace ttest_var = 0 if treat==0 & post_pair==0 

	gen sqft1000s = sqft / 1000 
	gen HDD1000s = HDD / 1000 
	gen CDD1000s = CDD / 1000 
	gen used_space1000s = used_space/ 1000
	gen real_capex_ti_sqft = real_capex_ti / sqft 
	gen real_capex_bldimp_sqft = real_capex_bldimp / sqft 
	gen nra1000s = nra / 1000
	

* Step 1: Label variables with the text you want to appear in the table *

	label var interaction "Cert*Post" 
	label var treat "1[treat=1]"
	label var logrealrentpersf "ln(Rent/Sq. Ft.) (\\$)"
	label var Covered_E "Benchmarking Law" 
	label var real_elecprice "Avg. Elec. Price (\\$/MWh)" 
	label var real_gasprice "Avg. Gas Price (\\$/Mcf)" 
	label var Unemployment "Unemployment" 
	label var age "Building Age" 
	label var age2 "Building Age Squared" 
	label var used_space "Used Space (Pct. Leased * Sq. Ft.)"
	label var used_space1000s "Used Space (Pct. Leased * Sq. Ft.) (1000s)"
	labe var percentleased "Percent Leased (\%)"
	label var real_capex_ti "Cap Exp. (Tenant Imp.)" 
	label var real_capex_ti_sqft "Cap Exp./Sq. Ft. (Tenant Imp.) (\\$)"
	label var real_capex_bldimp "Cap Exp. (Bldg. Imp.)"
	label var real_capex_bldimp_sqft "Cap Exp./Sq. Ft. (Bldg. Imp.) (\\$)"
	label var HDD "HDD" 
	label var HDD1000s "HDD (1000s)" 
	label var CDD "CDD" 
	label var CDD1000s "CDD (1000s)" 
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var logrealutilpersf "ln(Util/Sq. Ft.) (\\$)"
	label var rl_yr_rentpersf "Rent per Sq. Ft."
	label var rl_yr_utilpersf "Util. per Sq. Ft."
	label var yrbuilt "Year Built" 
	label var sqft "Square Feet"
	label var sqft1000s "Square Feet (1000s)" 
	label var nooffloors "Number of Floors"
	label var noofunits "Number of Units"
	label var noofbuildings "Number of Buildings"
	label var nra1000s "Net Rentable Area (1000s)"
	label var lastrenovatedyear "Last Renovated Year"
	label var latitude  "Latitude"
	label var longitude "Longitude"

*Step 2 Make a local with the variables I am doing the ttest for 
	loc covars "rl_yr_utilpersf rl_yr_rentpersf real_elecprice real_gasprice Unemployment HDD1000s CDD1000s used_space1000s percentleased real_capex_ti_sqft real_capex_bldimp_sqft sqft1000s yrbuilt Covered_E nooffloors noofunits noofbuildings nra1000s lastrenovatedyear latitude longitude"


*Step 3 Estimate means to store in matrices to put into tables 

	*Estimate the means and SDs 
	*clear stored estimates 
	eststo clear
	estpost sum `covars' if ttest_var==1
	
	matrix mean_cert=e(mean)
	matrix list mean_cert
	
	estpost sum `covars' if ttest_var==0
	matrix mean_uncert = e(mean) 
	matrix list mean_uncert 
	
	stddiff `covars', by(ttest_var)
	matrix stddiff = r(stddiff)
	matrix stddifftrans = stddiff'*100
	matrix list stddifftrans
	matrix colnames stddifftrans = rl_yr_utilpersf rl_yr_rentpersf real_elecprice real_gasprice Unemployment HDD1000s CDD1000s used_space1000s percentleased real_capex_ti_sqft real_capex_bldimp_sqft sqft1000s yrbuilt Covered_E nooffloors noofunits noofbuildings nra1000s lastrenovatedyear latitude longitude
	
*Step 4, run ttest 
	*Run ttest 
	estpost ttest `covars', by(ttest_var)
	
	
	
*Step 5 add stored means to ttest results 

	estadd matrix mean_cert
	estadd matrix mean_uncert
	estadd matrix stddifftrans
	
	esttab 
*Step 6 Locals for column names 
	
	   loc colnames " "\multicolumn{1}{c}{Mean Uncertified Pre-Matched-Cert}" "\multicolumn{1}{c}{Mean Certified Pre-Cert}" "\multicolumn{1}{c}{Diff.}" "\multicolumn{1}{c}{Std. Error}" "\multicolumn{1}{c}{Std. Diff (\%)}" "N" "

	
*Step 6 Output into table 

	esttab using "$results/12_balancetest_matched`i'_wmatches.tex",  label replace booktabs ///
	nonotes nogaps /// removes notes from bottom of table 
	nonumbers /// 
	nomtitles /// 
	alignment(SSSSS) ///
	star(* 0.05)  ///
	cells("mean_cert(fmt(%9.3f)) mean_uncert(fmt(%9.3f)) b(star fmt(%9.3f)) se(fmt(%9.3f)) stddifftrans(fmt(%9.3f))") ///
	title(Matched Sample: Balance Test \label{tab12matchedbalance`i'wmatches}) ///
	nonum compress noobs nobaselevels  collabels(`colnames') ///
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	addnote("Each column reports the annual average for certified buildings and uncertfied buildings prior to certification. For buildings that are never certified we define before" ///
						"certification by assigning the certification year of the matched certified building. In column ``Diff.'' * p$<$0.05 indicates statistically significant" /// 
						"differences between certified and uncertified buildings. Column ``Std. Diff (\%)'' presents the standardized percent bias which is defined as the difference" ///
						"in the sample means between two groups as a percentage of the square root of the average sample variance. While there is no optimal level of standardized bias," ///
						"empirical researchers often consider a standardized bias of 3–5\% to be sufficient \textcite{caliendo2008}. ")

}


						

			*Try doing balance test only for the building-level observation rather than 
	*The full sample 
	import delimited "$build/2021_6_29_maha_match_timeinvar_cal.csv", delimiter(";") clear
	*Take the data from long back to wide 
	keep propnum weight matchcalindexcontrol matchcalindextreat
	tempfile PropnumWeights
	save "`PropnumWeights'" 
*Merge the weight data back into the data 
	use "$build/NCREIF_CleanedForMatch2", clear
	
	*Estimate the standardized percent bias for the unmatched sample 
	gen ttest_var = . 
	replace ttest_var = 1 if treat==1 
	replace ttest_var = 0 if treat==0
	
	loc covars "mean_nooffloors mean_noofunits mean_noofbuildings mean_nra mean_sqft mean_yrbuilt mean_lastrenovatedyear mean_yrbuiltorlastren  mean_Covered_E mean_latitude mean_longitude"

	stddiff `covars', by(ttest_var)
	matrix stddiffun = r(stddiff)
	matrix stddifftransun = stddiffun'
	matrix stddifftransun = stddifftransun*100
	matrix list stddifftransun
	matrix colnames stddifftransun = mean_nooffloors mean_noofunits mean_noofbuildings mean_nra mean_sqft mean_yrbuilt mean_lastrenovatedyear mean_yrbuiltorlastren  mean_Covered_E mean_latitude mean_longitude
	
	
	use "$build/NCREIF_CleanedForMatch2", clear

	merge m:1 propnum using "`PropnumWeights'" 
	keep if _merge==3 	
	
	
	*Generate the post-variable for uncertified buildings to line up with the certification year of their
	*matched building 
	*within each pair generate the treatment year 
	*check that observations are unique 
	capture drop id_unique 
	bysort  matchcalindextreated treat year: gen id_unique = _n ==1 // al unique 
	drop id_unique
		
	gen ttest_var = . 
	replace ttest_var = 1 if treat==1 
	replace ttest_var = 0 if treat==0




* Step 1: Label variables with the text you want to appear in the table *

	*Label variables that we matched on for the balance table 
	label var mean_nooffloors "No. Floors"
	label var mean_noofunits "No. Units"
	label var mean_noofbuildings  "No. Buildings"
	label var mean_nra "Net Rentable Area"
	label var mean_sqft "Sqft."
	label var mean_yrbuilt "Year Built"
	label var mean_lastrenovatedyear "Last Renovated Year"
	label var mean_yrbuiltorlastren "Year Built or Last Renovated"
	label var mean_Covered_E "Benchmarking Law"
	label var mean_latitude "Latitude"
	label var mean_longitude "Longitude"

*Step 2 Make a local with the variables I am doing the ttest for 
	loc covars "mean_nooffloors mean_noofunits mean_noofbuildings mean_nra mean_sqft mean_yrbuilt mean_lastrenovatedyear mean_yrbuiltorlastren  mean_Covered_E mean_latitude mean_longitude"
	

*Step 3 Estimate means to store in matrices to put into tables 

	*Estimate the means and SDs 
	*clear stored estimates 
	eststo clear
	estpost sum `covars' if ttest_var==1
	
	matrix mean_cert=e(mean)
	matrix list mean_cert
	
	estpost sum `covars' if ttest_var==0
	matrix mean_uncert = e(mean) 
	matrix list mean_uncert 
	
	stddiff `covars', by(ttest_var)
	matrix stddiff = r(stddiff)
	matrix stddifftrans = stddiff'
	matrix stddifftrans = stddifftrans*100 
	matrix list stddifftrans
	matrix colnames stddifftrans = mean_nooffloors mean_noofunits mean_noofbuildings mean_nra mean_sqft mean_yrbuilt mean_lastrenovatedyear mean_yrbuiltorlastren  mean_Covered_E mean_latitude mean_longitude
	
	matrix num = stddifftrans - stddifftransun
	matrix list num
	matrix numden = J(1,11,0)
	matrix list numden 

	forvalues i = 1/11 {
		 matrix numden[1,`i']= num[1,`i']/stddifftransun[1,`i']
	}
	matrix list numden 

	mata
	numden = num :/ stddifftransun
	num 
	end
	matrix list pctchange 
	
*Step 4, run ttest 
	*Run ttest 
	estpost ttest `covars', by(ttest_var)
	
	
	
*Step 5 add stored means to ttest results 

	estadd matrix mean_cert
	estadd matrix mean_uncert
	estadd matrix stddifftrans
	estadd matrix stddifftransun
	
	esttab 
*Step 6 Locals for column names 
	
	   loc colnames " "\multicolumn{1}{c}{Mean Uncertified Pre-Matched-Cert}" "\multicolumn{1}{c}{Mean Certified Pre-Cert}" "\multicolumn{1}{c}{Diff.}" "\multicolumn{1}{c}{Std. Error}" "\multicolumn{1}{c}{Std. Diff (\%)}" "\multicolumn{1}{c}{Std. Diff Unmatched (\%)}" "N" "

	
*Step 6 Output into table 

	esttab using "$results/12_balancetest_matched2'_wmatches_matchedcovars.tex",  label replace booktabs ///
	nonotes nogaps /// removes notes from bottom of table 
	nonumbers /// 
	nomtitles /// 
	alignment(SSSSS) ///
	star(* 0.05)  ///
	cells("mean_cert(fmt(%9.3f)) mean_uncert(fmt(%9.3f)) b(star fmt(%9.3f)) se(fmt(%9.3f)) stddifftrans(fmt(%9.3f)) stddifftransun(fmt(%9.3f))") ///
	title(Matched Sample: Balance Test \label{tab12matchedbalance`i'wmatches}) ///
	nonum compress noobs nobaselevels  collabels(`colnames') ///
	stats(N, fmt(%9.0fc)) /// adds comma to Observation number
	addnote("Each column reports the annual average for certified buildings and uncertfied buildings prior to certification. For buildings that are never certified we define before" ///
						"certification by assigning the certification year of the matched certified building. In column ``Diff.'' * p$<$0.05 indicates statistically" /// 
						"significant differences between certified and uncertified buildings. Column ``Std. Diff (\%)'' presents the standardized percent bias which is defined as" ///
						"the difference in the sample means between two groups as a percentage of the square root of the average sample variance." ///
						"While there is no optimal level of standardized bias, empirical researchers often consider a standardized bias of 3–5\% to be sufficient \textcite{caliendo2008}. ")

